<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博详情页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }

        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            max-width: 800px;
            margin: 0 auto;
            border: 1px solid #e0e0e0;
        }

        h1, h2, p, h4 {
            margin: 0;
        }

        h1 {
            text-align: center;
            font-size: 28px;
            color: #333;
        }

        h2 {
            color: mediumvioletred;
            margin-bottom: 15px;
            font-size: 24px;
        }

        p {
            color: #666;
            margin-bottom: 15px;
            font-size: 16px;
        }

        hr {
            margin: 20px 0;
            border-color: #e0e0e0;
        }

        .comment {
            margin-bottom: 15px;
            padding: 10px;
            background-color: #f9f9f9;
            border-radius: 8px;
        }

        .comment span {
            color: #ff4500;
            font-weight: bold;
        }

        .comment span.right {
            float: right;
            color: #666;
        }

        textarea {
            width: 100%;
            height: 120px;
            margin-bottom: 15px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 16px;
        }

        input[type="button"] {
            width: 100%;
            padding: 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 18px;
            transition: background-color 0.3s ease;
        }

        input[type="button"]:hover {
            background-color: #0056b3;
        }

        .loading {
            text-align: center;
            margin: 20px 0;
            font-size: 18px;
            color: #666;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>微博详情页面</h1>
    <h2><span style="color:mediumvioletred">{{weibo.nickname}}</span>: {{weibo.content}}</h2>
    <p>发布于: {{weibo.created}}</p>
    <hr>
    <div v-for="c in arr" class="comment">
        <h4>
            <span style="color:#ff4500">{{c.nickname}}评论道:</span> {{c.content}}
            <span class="right">发布于：{{c.created}}</span>
        </h4>
    </div>

    <textarea cols="30" rows="3" placeholder="评论内容..." v-model="comment.content"></textarea>
    <input type="button" value="评论" @click="insert()">
    <div v-if="loading" class="loading">加载中...</div>
</div>

<!-- 引入axios和vue的框架文件 -->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el: ".container",
        data: {
            weibo: {},
            comment: { content: "" },
            arr: [],
            loading: true
        },
        methods: {
            insert() {
                // 发布评论时需要把当前浏览的微博id添加到评论对象中
                v.comment.weiboId = v.weibo.id;
                // 发请求,发布评论
                axios.post("/v1/comment/insert", v.comment)
                    .then(function (response) {
                        if (response.data === 1) {
                            location.reload(); // 刷新页面
                        } else {
                            alert("请先登录");
                            location.href = "/login.html";
                        }
                    })
                    .catch(function (error) {
                        console.error(error);
                        alert("评论失败，请稍后再试");
                    });
            }
        },
        created: function () {
            this.loading = true;
            axios.get("/v1/weibo/selectById" + location.search)
                .then(function (response) {
                    v.weibo = response.data;
                })
                .catch(function (error) {
                    console.error(error);
                    alert("加载微博失败，请稍后再试");
                });

            // 发请求获取当前微博相关的评论
            axios.get("/v1/comment/selectByWeiboId" + location.search)
                .then(function (response) {
                    v.arr = response.data;
                    v.loading = false;
                })
                .catch(function (error) {
                    console.error(error);
                    alert("加载评论失败，请稍后再试");
                    v.loading = false;
                });
        }
    });
</script>

</body>
</html>
